package cn.topevery.garden.dal.logic;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.constant.StringConstant;
import cn.topevery.garden.dal.dao.ProjBasicInfoDao;
import cn.topevery.garden.entity.dto.ProjBasicInfoDTO;
import cn.topevery.garden.entity.dto.ProjMoneyDTO;
import cn.topevery.garden.entity.po.ProjBasicInfo;
import cn.topevery.garden.entity.po.ProjFiles;
import cn.topevery.garden.entity.po.ProjPlan;
import cn.topevery.garden.entity.query.ProjBasicInfoQuery;
import cn.topevery.garden.entity.query.ProjPaymentQuery;
import cn.topevery.garden.entity.query.ProjPlanQuery;
import cn.topevery.garden.entity.query.ProjRequestFundsPlanQuery;
import cn.topevery.garden.entity.vo.*;
import cn.topevery.garden.framework.base.BaseLogic;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author LiBo
 * @Date 2022/11/2 15:19
 */
@Service
public class ProjBasicInfoLogic extends BaseLogic<ProjBasicInfo> {

    @Autowired
    private Mapper mapper;

    @Autowired
    ProjBasicInfoDao projBasicInfoDao;

    @Autowired
    ProjPlanLogic projPlanLogic;

    @Autowired
    ProjSituationRecordLogic projSituationRecordLogic;

    @Autowired
    ProjRequestFundsPlanLogic projRequestFundsPlanLogic;

    @Autowired
    ProjContractLogic projContractLogic;

    @Autowired
    ProjPaymentLogic projPaymentLogic;

    @Autowired
    ProjFilesLogic projFilesLogic;

    @Override
    public BaseEntityDao<ProjBasicInfo, Integer> getDao() {
        return projBasicInfoDao;
    }

    /**
     * 根据自增id保存或者更新
     *
     * @param userId
     * @param dto
     */
    public void saveOrUpdate(String userId, ProjBasicInfoDTO dto) {
        projBasicInfoDao.saveOrUpdate(userId, dto);
        //  保存附件信息
        if (ObjectUtil.isNotEmpty(dto.getFiles())) {
            List<ProjFiles> files = dto.getFiles().stream().filter(i -> ObjectUtil.isEmpty(i.getId())).collect(Collectors.toList());
            files.stream().forEach(i -> i.setProjId(dto.getProjId()));
            if (ObjectUtil.isNotEmpty(files)) {
                projFilesLogic.insert(userId, files);
            }
        }
        //  删除附件
        if (ObjectUtil.isNotEmpty(dto.getDeleteFileIds())) {
            projFilesLogic.delete(userId, dto.getDeleteFileIds());
        }
    }

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<ProjBasicInfoVO> listPage(ProjBasicInfoQuery query) {
        return projBasicInfoDao.listPage(query);
    }

    /**
     * 根据自增ID获得详情
     *
     * @param id
     * @return
     */
    public ProjBasicInfoAndFilesVO getBasicInfoVOById(Integer id) {
        ProjBasicInfo basicInfo = getEntity(id);
        if (ObjectUtil.isEmpty(basicInfo)) {
            return null;
        }
        ProjBasicInfoAndFilesVO basicInfoVO = mapper.map(basicInfo, ProjBasicInfoAndFilesVO.class);
        List<ProjFilesVO> baseInfoFiles = projFilesLogic.listByProjIdAndStageTypeCode(basicInfo.getProjId(), StringConstant.ProjStageFileCode.ProjBasic);
        basicInfoVO.setFiles(baseInfoFiles);
        return basicInfoVO;
    }

    public ProjDetailVO getProjDetailById(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new RuntimeException("传入的ID不能为空");
        }
        ProjDetailVO projDetailVO = new ProjDetailVO();
        // 获取项目详情
        ProjBasicInfo basicInfo = getEntity(id);
        if (ObjectUtil.isNotEmpty(basicInfo)) {
            BeanUtils.copyProperties(basicInfo, projDetailVO);
            String projId = projDetailVO.getProjId(); // 项目ID
            // 查询项目计划
            List<ProjPlanIndexVO> projPlanList = projPlanLogic.listByProjId(projId);
            projDetailVO.setPlanList(projPlanList);

            // 查询项目资金情况
            ProjFundSituationVO projFundSituationVO = this.selectProjFundSituationByProjId(projId);
            // 设置项目总投资（万元）
            projFundSituationVO.setProjTotalMoney(basicInfo.getEstimateTotalMoney());
            projDetailVO.setProjFundSituationVO(projFundSituationVO);

            // 查询请款记录
            ProjRequestFundsPlanQuery requestFundsPlanQuery = new ProjRequestFundsPlanQuery();
            requestFundsPlanQuery.setProjId(projId);
            List<ProjRequestFundsPlanVO> requestFundsList = projRequestFundsPlanLogic.list(requestFundsPlanQuery);
            if (CollUtil.isNotEmpty(requestFundsList)) {
                projDetailVO.setRequestFundsList(requestFundsList);
            }

            // 查询支付记录
            ProjPaymentQuery paymentQuery = new ProjPaymentQuery();
            List<ProjPaymentVO> paymentList = projPaymentLogic.list(paymentQuery);
            if (CollUtil.isNotEmpty(paymentList)) {
                projDetailVO.setPaymentList(paymentList);
            }
        }
        return projDetailVO;
    }

    public ProjFundSituationVO selectProjFundSituationByProjId(String projId) {
        ProjFundSituationVO projFundSituationVO = new ProjFundSituationVO();
        ProjMoneyDTO projMoneyDTO = new ProjMoneyDTO();
        projMoneyDTO.setProjId(projId);
        // 查询项目概算金额
        ProjSituationRecordMoneyVO projSituationRecordMoneyVO = projSituationRecordLogic.getEstimateMoney(projMoneyDTO);
        if (ObjectUtil.isNotEmpty(projSituationRecordMoneyVO)) {
            // 项目概算金额(万元)
            projFundSituationVO.setEstimateTotalMoney(projSituationRecordMoneyVO.getEstimateTotalMoney());
        }

        // 查询合同金额
        ProjContractMoneyVO projContractMoneyVO = projContractLogic.getContractMoney(projMoneyDTO);
        if (ObjectUtil.isNotEmpty(projContractMoneyVO)) {
            // 已签订合同金额(万元)
            projFundSituationVO.setContractMoney(projContractMoneyVO.getContractMoney());
        }

        // 查询支付金额
        ProjPaymentMoneyVO projPaymentMoneyVO = projPaymentLogic.getPaymentMoney(projMoneyDTO);
        if (ObjectUtil.isNotEmpty(projPaymentMoneyVO)) {
            // 支付金额（万元）
            projFundSituationVO.setPaymentMoney(projPaymentMoneyVO.getPaymentMoney());
        }

        return projFundSituationVO;
    }

    /**
     * 根据projId获得实体类
     *
     * @param projId
     * @return
     */
    public ProjBasicInfo getByProjId(String projId) {
        return projBasicInfoDao.getByProjId(projId);
    }
}
